/*
* $Id: api.h,v 1.69 2008/03/10 05:31:10 sunny Exp $
*/
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: api.h

       Version: 0.3.0

       Usage: all function definition

       Platform: U-NET01 DK with Keil 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124, C8051F340

               UBEC: UZ2400, UZ2410

       Note : LED 4~7 Only for U-zig interface

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/

#ifndef _API_H_
#define	_API_H_

#include "frame.h"
#include "msg.h"
#include "data.h"

/***************************************************************
                 temperate function
****************************************************************/



/**************************************************************

	User Application Function Entry

**************************************************************/

void SystemInit(void);
//void SystemReset(void);

UINT16 GetMacAddress(void);
void AppInit( void );
void AppMain( void );
UINT8 AppProcUnetRxData( UNET_HEADER *MsgPtr, UINT8 Length );
UINT8 AppProcUartRxData( UINT8 *MsgPtr, UINT8 Length );

/**************************************************************

	MAC Main Function

**************************************************************/

UINT8 unet_tx_bcn_req(void);
UINT8 unet_tx_bcn(void);
UINT8 unet_rx_bcn_ind(MAC_BCN_PKT *BcnPkt, UINT8 PktLength);
UINT8 unet_tx_asso_req(void);
UINT8 unet_rx_asso_req(MCP_ASSO_REQ *MsgPtr);
void unet_rx_asso_rsp(MCP_ASSO_RSP *RsPtr);
UINT8 unet_rx_data_ind(UNET_HEADER **MsgPtr, UINT8 Length);
UINT8 unet_tx_disasso(UINT8 *DST_MAC, UINT8 Reason, UINT16 NewParent);
UINT8 unet_rx_disasso(MCP_DISASSO_NTF *CmdPtr);
void unet_active_scan(void);

/**************************************************************

	UNET Main Function

**************************************************************/

UINT8 UNET_INIT(UINT16 PanId, UINT8 Channel, UINT16 NewParent, UINT8 MaxChild, UINT8 MaxRouter, UINT8 MaxDepth);
UINT8 UNET_ESTABLISH(void);
UINT8 UNET_JOIN(void);
void UNET_GET_INFOMATION(void);

UINT8 CHECK_UNET_DATA(void);
UINT8 *RECV_FROM_UNET(UINT8 *Length);

#ifdef I_Am_Coordinator
UINT8 UNET_KICK(UINT16 ParentAddr, UINT16 KickChildAddr, UINT16 NewParent);
UINT8 UNET_REMOTE_RESET(UINT16 DstAddr, UINT8 maxchild, UINT8 maxrouter, UINT8 maxdepth);
void UNET_GET_TABLE(void);

UINT8 UNET_QUERY(UINT16 DstAddr);
UINT8 RxUnetJoinReport(UNET_JOINREPORT_FRAME* MsgPtr);
UINT8 RxUnetLeaveReport(UNET_LEAVE_FRAME* MsgPtr);
UINT8 RxUnetRspTable(UNET_TAB_FRAME* MsgPtr);
UINT8 UnetTxCheck(UINT16 dstaddr);
#endif

UINT8 UNET_TX16(UINT16 DstAddr, UINT8 *DataPtr, UINT8 Length);
UINT8 UNET_TX64(UINT8* DstMAC, UINT8* DataPtr, UINT8 Length);
UINT8 UNET_ADDR16_64(UINT16 DstAddr);


UINT8 UNET_LEAVE(void);

UINT8 TxUnetJoinReport(UINT8* JoinChildMACAddr, UINT16 JoinChildNWKAddr);
UINT8 TxUnetLocalQuery(UINT16 DstAddr, UINT32 Interval, UINT8 Switch);
UINT8 TxUnetLocalTalk(void);
UINT8 TxUnetLocalTalkAlert(UINT16 nwkAddress);
UINT8 TxUnetReqTable(UINT16 dst_nwk_address);
UINT8 TxUnetMARsp(UINT16 DstAddr);
#if 0
UINT8 TxUnetCleanSpace(UINT16 parentaddr, UINT16 childaddr);
#endif
UINT8 TxUnetLeaveReport(UINT16 LeaveChildAddr);
UINT8 TxUnetRspQueryRelay(void);

UINT8 RxUnetFrameParsing(UNET_HEADER* RxPtr);
void RxUnetData(UNET_HEADER* RxPtr, UINT8 Length);
void RxUnetReset(void);
UINT8 RxUnetKick(UNET_KICK_FRAME *MsgPtr);
UINT8 RxUnetRspMA(UNET_RSP_MA_FRAME* MsgPtr);
UINT8 RxUnetLeave(UNET_HEADER* MsgPtr);
#if 0
UINT8 RxUnetCleanSpace(UNET_LEAVE_FRAME* MsgPtr);
#endif
UINT8 RxUnetRspQuery(UNET_QUERY_FRAME* MsgPtr);
#ifdef I_Am_Coordinator
UINT8 RxUnetMACRouting(UNET_MAC_ROUTE_FRAME* MsgPtr);
#endif
UINT8 RxUnetRspQueryRelay(UNET_QUERY_FRAME* RxPtr);
UINT8 RxUnetLocalQuery(UNET_LOCAL_QUERY_FRAME* MsgPtr);
void RxUnetLocalTalk(UNET_LOCAL_TALK_FRAME* MsgPtr);
UINT8 RxUnetReqTable(void);
void PollCheck(UINT16 *nwk_address);

void unet_initial(void);
void init_unet_vars(void);
void unet_proc(void);
INT8 unet_queue_proc(UINT8 frequency, MSG_QUEUE *QueuePtr);
UINT8 unet_msg_dispatch(void);	// REENTRANT;
UINT16 unet_tree_routing(UINT16 DstAddr);

UINT8 unet_check_child(UINT16 NwkAddr, struct _child_list_ **CurrPtr);
void clean_branch(UINT16 Caddr, UINT16 Paddr);
void clear_table(void **HeadAddr);
UINT16 compute_Cskip(INT8 depth);
void NetworkReset(void);
UINT16 power(UINT16 number, UINT8 power);
void UnetRecovery(UINT16 failure_address);
void UnetChildAliveCheck(void);



#ifdef CHECKSUM
UINT8 UnetCheckSum(UINT8* DataPtr, UINT8 Length);
BOOL UnetCheckSumCertification(UINT8* DataPtr, UINT8 Length, UINT8 CSM);
UINT32 ReceivedNumber;
#endif

#if DEBUG_FUNC

#ifdef I_Am_Coordinator
void dbg_rssi_query(void);
uint8 dbg_rx_node_info_rsp(UNET_NODE_INFO_FRAME* msgptr);

void dbg_child_number_set(uint8 child_number, uint8 isMonitor);
void dbg_child_monitor(void);
#endif

uint8 dbg_tx_node_info_req(uint16 dst_addr);
uint8 dbg_rx_node_info_req(void);
uint8 dbg_tx_node_info_rsp(void);

#endif


/**************************************************************

	Rf Function

**************************************************************/

void UzGetIntStatus(void);

void UzInit(void);

void UzEnablePA(void);

void UzDisablePA(void);

void UzSetTxPower(INT8 dB);	// dB Range: 0 ~ -40

void UzSetChannel(UINT8 NewChannel);

UINT8 UzReadRSSI(void);

void UzSetMacAddress(UINT8 *MacAddress);

void UzSetPanId(UINT16 PanId);

void UzSetNwkAddr(UINT16 NwkAddr);

void UzSoftReset(void);

void UzRxOnlyBeacon(void);

void UzRxAllFrame(void);

void UzRxFlush(void);

void UzSleep(void);

UINT8 UzTx(UINT8 *DataPtr, UINT8 Length, UINT8 mactpye, UINT8 unettpye);
UINT8 UzSecureTx(UINT8 SecMode, UINT8 *SecKey, UINT8 *DataPtr, UINT8 Length, UINT8 HeaderLength);
UINT8 UzSecureRx(UINT8 SecMode, UINT8 *SecKey, UINT8 *InBuff);

void UzSetCoordinator(void);

void UzEnabTurboMode(void);

void UzDisTurboMode(void);


/*******************************************************************************

	UBEC chip RESET and SEN control

*******************************************************************************/

void UBEC_Activate_RESET(void);

void UBEC_Deactivate_RESET(void);

void UBEC_Activate_SEN(void);

void UBEC_Deactivate_SEN(void);


/**************************************************************

	SPI Functions

**************************************************************/

void InitSPI(void);

#ifdef IOUZ2410
#define UzChipReset()

#define spi_sw(Address, Value)		*((XUINT8 *)(0xF800 + (Address))) = Value

#ifdef UZ2410_VERSION_3
	UINT8 spi_sr(UINT8 Address);
#else
	#define spi_sr(Address)		*((volatile XUINT8 *)(0xF800 + (Address)))
#endif // UZ2410_VERSION_3

#define spi_lw(Address, Value)		*((XUINT8 *)(0xFC00 + (Address))) = Value

#define spi_lr(Address)			*((volatile XUINT8 *)(0xFC00 + (Address)))

#else
void UzChipReset(void);

UINT8 spi_rw(UINT8 c);

void spi_sw(UINT8 Address, UINT8 Value);

UINT8 spi_sr(UINT8 Address);

void spi_lw(UINT16 Address, UINT8 Value);

UINT8 spi_lr(UINT16 Address);
#endif // #ifdef IOUZ2410

void spi_fill_fifo(UINT16 Address, UINT8 *DataPtr, UINT8 Length);

void spi_rd_rx_fifo(UINT8 *DataPtr, UINT8 buf_size);


/**************************************************************

	Uart Functions

**************************************************************/
#ifdef CONSOLE

#define uart_fifo_empty(fifo)		((fifo)->front == (fifo)->rear)
#define isUartRxDataReady()		(!uart_fifo_empty(&(UART_BUFF.rx)))

#define set_uart_tx_busy(uart)		(uart)->tx_busy = 1
#define clear_uart_tx_busy(uart)	(uart)->tx_busy = 0
#define is_uart_tx_busy(uart)		(uart)->tx_busy

void ProcUartRxData(void);
void uart_rx_isr(void);
void uart_tx_isr(void);

void uart_putc(UINT8 c);		// called by putchar() and uart_tx_isr()
UINT8 uart_getc(void);			// called by uart_rx_isr()

void InitUART(void);
void InitUartVars(void);

UINT8 getch(void);
//int puts(char *s);

void putdata(UINT8 UARTWLength, UINT8* DataPtr);
void print_hex(UINT8 c);
void print_ulong(unsigned long value);
void print_long(long value);
#define print_uint(value)		print_ulong((unsigned long)(value))
#define print_int(value)		print_long((long)(value))

#endif
/**************************************************************

	1ms Timer Functions

**************************************************************/
void init_1ms_timer(void);
void InitTimerVars(void);
void UpdateTimerVars(void);

TIMER *setup_timer(UINT32 count, UINT8 close_timer_flag, void (*fn)(), void *fn_param);

#if defined(IOXC800)
void update_timer_chain(void);
#else
void update_timer_chain(void) REENTRANT;
#endif

UINT8 is_timer_expired(TIMER *target_tm);

UINT8 reset_timer(TIMER *target_tm);

void close_timer(TIMER *tm);

void WaitMS(UINT16 count);

/**************************************************************

	LED Functions

**************************************************************/

#define	LED_CTRL_ON			0
#define	LED_CTRL_OFF			1

#define	_IS_LED_ON(p)			((p) == LED_CTRL_ON)
#define	_LED_OFF(p)			((p) = LED_CTRL_OFF)
#define	_LED_ON(p)			((p) = LED_CTRL_ON)
#define _LED_TOGGLE(p)			((p) ^= 1)

#ifdef LINK_LED
	#define _Link_LED_On()		_LED_ON(LINK_LED)
	#define _Link_LED_Off()		_LED_OFF(LINK_LED)
	#define _Link_LED_Toggle()	_LED_TOGGLE(LINK_LED)
	#define _Is_Link_LED_On()	_IS_LED_ON(LINK_LED)
#else
	#define _Link_LED_On()
	#define _Link_LED_Off()
	#define _Link_LED_Toggle()
	#define _Is_Link_LED_On()	0
#endif // #ifdef LINK_LED

void Link_LED_On(void);
void Link_LED_Off(void);
void Link_LED_Toggle(void);
BOOL Is_Link_LED_On(void);

#ifdef RXTX_LED
	#define _RxTx_LED_On()		_LED_ON(RXTX_LED)
	#define _RxTx_LED_Off()		_LED_OFF(RXTX_LED)
	#define _RxTx_LED_Toggle()	_LED_TOGGLE(RXTX_LED)
	#define _Is_RxTx_LED_On()	_IS_LED_ON(RXTX_LED)
#else
	#define _RxTx_LED_On()
	#define _RxTx_LED_Off()
	#define _RxTx_LED_Toggle()
	#define _Is_RxTx_LED_On()	0
#endif // #ifdef RXTX_LED

void RxTx_LED_On(void);
void RxTx_LED_Off(void);
void RxTx_LED_Toggle(void);
BOOL Is_RxTx_LED_On(void);

#ifdef UART_LED
	#define _UART_LED_On()		_LED_ON(UART_LED)
	#define _UART_LED_Off()		_LED_OFF(UART_LED)
	#define _UART_LED_Toggle()	_LED_TOGGLE(UART_LED)
	#define _Is_UART_LED_On()	_IS_LED_ON(UART_LED)
#else
	#define _UART_LED_On()
	#define _UART_LED_Off()
	#define _UART_LED_Toggle()
	#define _Is_UART_LED_On()	0
#endif // #ifdef UART_LED

void UART_LED_On(void);
void UART_LED_Off(void);
void UART_LED_Toggle(void);
BOOL Is_UART_LED_On(void);

#ifdef POLLING_LED
	#define _User_LED_On()	_LED_ON(POLLING_LED)
	#define _User_LED_Off()	_LED_OFF(POLLING_LED)
	#define _User_LED_Toggle()	_LED_TOGGLE(POLLING_LED)
	#define _Is_User_LED_On()	_IS_LED_ON(POLLING_LED)
#else
	#define _User_LED_On()
	#define _User_LED_Off()
	#define _User_LED_Toggle()
	#define _Is_User_LED_On()	0
#endif // #ifdef POLLING_LED

void User_LED_On(void);
void User_LED_Off(void);
void User_LED_Toggle(void);
BOOL Is_User_LED_On(void);

/*******************************************************************************

	MSG Function

********************************************************************************/

void  *MM_Alloc(UINT8 size);

void MM_Free(void *allocaddr);

void MM_Init(MSG_QUEUE *queueaddr);

#define MSG_Alloc(size) MM_Alloc(size)

#define MSG_AllocType(type) MM_Alloc(sizeof(type))

#define MSG_Free(allocaddr) MM_Free(allocaddr)

#define MSG_Init(queueaddr) MM_Init(queueaddr)

INT8 MSG_Add(void * Msg, UINT8 msgType, MSG_QUEUE * QueuePtr, UINT8 Length);

INT8 MSG_Fetch(MSG_QUEUE *QueuePtr);

#define MSG_Copy(dest, src, len) memcpy((void *)(dest), (const void *)(src), (size_t)(len));

void *insert_node(void **TailAddr, UINT8 TypeSize);

void delete_node(void **TailAddr, void **DelAddr);

/*******************************************************************************

	Debug Function

********************************************************************************/


/*******************************************************************************

	Endian Function

********************************************************************************/
#if BIGENDIAN
	void SWAPINT32(UINT32* Value);
	void SWAPINT16(UINT16* Value);
	void Endian_Modify(UINT8 *DataPtr,UINT8 mactype,UINT8 unettype);

	void BE_MAC_DATA_PKT(UINT8 *DataPtr);
	void BE_MAC_BCN_PKT(UINT8 *DataPtr);
	void BE_MCP_BCN_REQ(UINT8 *DataPtr);
	void BE_MCP_ASSO_REQ(UINT8 *DataPtr);
	void BE_MCP_ASSO_RSP(UINT8 *DataPtr);
	void BE_MCP_DISASSO_NTF(UINT8 *DataPtr);
#else
	#define SWAPINT32(Value)
	#define SWAPINT16(Value)
	#define Endian_Modify(DataPtr, mactype, unettype)
#endif

#endif // #ifndef _API_H_
